﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace SHomeWorkshop.LunarSF.StudentInfosManager.Converters
{
    [ValueConversion(typeof(string), typeof(int))]
    public class SourceOfFamilyIncomeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return -1;
            var text = value as string;
            if (string.IsNullOrWhiteSpace(text)) return -1;

            int _value;
            if(int.TryParse(value as string,out _value))
            {
                return _value - 1;
            }
            return ((int)value) - 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (((int)value) + 1).ToString();
        }

        private static SourceOfFamilyIncomeConverter converter = new SourceOfFamilyIncomeConverter();

        public static SourceOfFamilyIncomeConverter Instance
        {
            get { return converter; }
        }
    }
}
